package com.hzit.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.entity.SpecificationOption;
import com.hzit.entity.TypeTemplate;
import com.hzit.mapper.TypeTemplateMapper;
import com.hzit.service.ISpecificationOptionService;
import com.hzit.service.ITypeTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wf
 * @since 2023-08-07
 */
@Service
public class TypeTemplateServiceImpl extends ServiceImpl<TypeTemplateMapper, TypeTemplate> implements ITypeTemplateService {

	@Autowired
	private ISpecificationOptionService optionService;
	//1. 根据模板id查询出规格及规格列表
	@Override
	public List<Map> findSpecListByTemplateId(Long id) {
		//1.1 查询出模板对象
		TypeTemplate template = this.getById(id);
		//1.2 得到规格对象集合的字符串形式
		String specIds = template.getSpecIds();
		//1.3 转换为List<Map>格式
		List<Map> maps = JSON.parseArray(specIds, Map.class);
		//1.4 遍历map
		for (Map map : maps) {
			//1.4.1 得到规格id
			String specId = map.get("id").toString();
			//1.4.2 根据模板id这个外键作为规格选项
			List<SpecificationOption> options = optionService.list(new QueryWrapper<SpecificationOption>().eq("spec_id", specId));
			//1.4.3 将上面的options与map绑定
			map.put("options",options);
		}

		return maps;
	}
}
